package com.wumart.scaffold.web.base.datasource;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.wumart.scaffold.web.base.datasource.property.ConfigurationProperty;
import com.wumart.scaffold.web.base.datasource.property.DbConfigProperty;
import lombok.Data;
import org.apache.ibatis.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @author wangwc@wumart.com
 * @version v1.0.0
 * @date 2021/1/21 11:51
 */
@Data
@Configuration
public class MyBatisCommonConfig {

    @Value("${local.mybatis-plus.type-aliases-package}")
    private String typeAliasesPackage;
    @Value("${local.mybatis-plus.enum-package}")
    private String enumPackage;

    @Autowired
    private ConfigurationProperty configurationProperty;
    @Autowired
    private DbConfigProperty dbConfigProperty;

    public PaginationInnerInterceptor mysqlPaginationInnerInterceptor(){
        return new PaginationInnerInterceptor(DbType.MYSQL);
    }

    public PaginationInnerInterceptor oraclePaginationInnerInterceptor(){
        return new PaginationInnerInterceptor(DbType.ORACLE);
    }

    public MybatisPlusInterceptor localMybatisPlusInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.setInterceptors(Arrays.asList(mysqlPaginationInnerInterceptor()));
        return mybatisPlusInterceptor;
    }


    public MybatisConfiguration localMybatisConfiguration() {
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        mybatisConfiguration.setLogImpl(configurationProperty.getLogImpl());
        return mybatisConfiguration;
    }

    public GlobalConfig.DbConfig locaDbConfig() {
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setLogicDeleteField(dbConfigProperty.getLogicDeleteFiled());
        dbConfig.setLogicDeleteValue(dbConfigProperty.getLogicDeleteValue());
        dbConfig.setLogicNotDeleteValue(dbConfigProperty.getLogicNotDeleteValue());
        return dbConfig;
    }

    public GlobalConfig localGlobalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setDbConfig(locaDbConfig());
        return globalConfig;
    }

    protected MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBeanBuilder() {
        MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        sessionFactory.setConfiguration(localMybatisConfiguration());
        sessionFactory.setGlobalConfig(localGlobalConfig());
        sessionFactory.setPlugins(localMybatisPlusInterceptor());
        return sessionFactory;
    }

}
